package com.lilicould.controller;

import com.lilicould.entity.LeaseContract;
import com.lilicould.service.impl.ContractServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/contract/edit")
public class ContractEditController extends HttpServlet{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        String contractId = req.getParameter("contractId");
        String signDate = req.getParameter("signDate");
        String startDate = req.getParameter("startDate");
        int leaseTerm = Integer.parseInt(req.getParameter("leaseTerm"));
        double deposit =  Double.parseDouble(req.getParameter("deposit"));
        double rent = Double.parseDouble(req.getParameter("rent"));
        String managerId = req.getParameter("managerId");

        // 封装参数
        LeaseContract leaseContract = new LeaseContract(contractId, signDate, startDate, leaseTerm, deposit, rent, managerId);

        // 获取服务层接口
        ContractServiceImpl contractService = new ContractServiceImpl();

        boolean result = contractService.updateContract(leaseContract);

        if (result) {
            req.setAttribute("success", "更新成功");
            req.getRequestDispatcher("/page/success.jsp").forward(req, resp);
        } else {
            req.setAttribute("error", "更新失败");
            req.getRequestDispatcher("/page/err.jsp").forward(req, resp);
        }
    }
}
